<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" type="text/css" href="../../../css/footer.css" />
		<script src="../../../js/jquery-2.2.3.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../../js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/jquery-validate.js" type="text/javascript"></script>
		<!-- <link rel="stylesheet" type="text/css" href="css/integrel.css" /> -->
		<style type="text/css">
			/*修改底部字体颜色*/
			
			.foot:nth-child(2) {
				color: #e83023;
			}
		</style>
		<script src="../../../js/flexible.min.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="wrap">
			<div id="main">
				<div id="header">
					<!--<a id="headImg" href="javascript:history.go(-1)"><img class="headImg" src="../../../images/12.png" /></a>-->
					<p id="headTitle">正在使用微信支付...</p>
				</div>
			</div>

		</div>

	</body>
	
	<script>
		var tokenzb = $.cookie("tokenRh");
		var memberId;
		var orderSn;
		var code = "";
		var state = "";
		var url = window.location.href;
		if(url.indexOf("code") > 0 )
		{
			code = url.split("?")[1].split("&")[0].split("=")[1];
			state = url.split("?")[1].split("&")[1].split("=")[1];
			if(state != null){
				orderSn = state;
			}
		}else{
			orderSn = url.split("?")[1];
		}
		
		if(!tokenzb) {
			window.location.href = "login.html";
		}
		function callpay()
		{	
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
			
		}
		
		
		function jsApiCall(){
			$.ajax({
				type: "GET",
				url: "http://47.104.13.109/pay-service/weixinPayHtml?orderSn="+orderSn+"&memberId="+memberId,
				async: true,
				success: function(data) {
					
					data = data.substr(0,data.length-1);
					data = data.substr(1,data.length);
					datas = data.split(",");
					var appId = datas[0].split("=")[1];
					var nonceStr = datas[1].split("=")[1];
					var package = datas[2].split("=")[1]+"="+datas[2].split("=")[2];
					var paySign = datas[3].split("=")[1];
					var signType = datas[4].split("=")[1];
					var timeStamp = datas[5].split("=")[1];
					//alert("appId="+appId+"nonceStr="+nonceStr+"package="+package+"signType="+signType+"timeStamp="+timeStamp);
		
					WeixinJSBridge.invoke(
			             'getBrandWCPayRequest', {
			                 "appId" : appId,     
			                 "timeStamp": timeStamp,    
			                 "nonceStr" : nonceStr,
			                 "package" : package,     
			                 "signType" : signType,  
			                 "paySign" : paySign
			             },function(res){
			              if(res.err_msg == "get_brand_wcpay_request:ok"){  
			                  alert("微信支付成功!");
			                  window.location.href="pay_stu.html?out_trade_no="+orderSn+"&trade_status=TRADE_SUCCESS";
			              }else if(res.err_msg == "get_brand_wcpay_request:cancel"){  
			                  alert("用户取消支付!");
			                  window.location.href="pay_stu.html?out_trade_no="+orderSn+"&trade_status=shibai";
			              }else{  
			            	 //alert(JSON.stringify(res));
			            	 //alert(res.err_msg);
			                 alert("支付失败!");
			                 window.location.href="pay_stu.html?out_trade_no="+orderSn+"&trade_status=shibai";
			              }  
			    	});
				},
				error: function(e) {
					 alert("支付失败!");
				}
			});
			//WeixinJSBridge.invoke('getBrandWCPayRequest',{pay},function(res){  if( 'get_brand_wcpay_request:ok'==res.err_msg ) { alert('支付成功'); }else{ alert('支付失败'); }  location.href='www.fagniu.com/visafn/order_detail.html?ordersn=2961103561';  });
		}
		
		function currentyf() {
					$.ajax({
						cache: false,
						type: "GET",
						url: "http://47.104.13.109/member-service/member/mine/member?token=" + tokenzb,
						dataType: "JSON",
						async: false,
						error: function(error) {
							$.removeCookie('tokenzb', {
								expires: 7,
								path: '/'
							});
						},
						success: function(data) {
							console.log(data);
							memberId = data.id;
						},error: function(e) {
						 	alert("获取会员信息失败,请返回上一级稍后再试！");
						}
					});
				}
		
				function cunchu() {
					$.ajax({
						cache: false,
						type: "GET",
						url: "http://47.104.13.109/pay-service/setOpenId?code="+code+"&memberId="+memberId,
						async: false,
						error: function(error) {
						},
						success: function(data) {
						}
					});
				}
			window.onload=function(){
				currentyf();
				if(code != ""){
					cunchu();
				}
				setTimeout("callpay()",500);
				
			}

	</script>
</html>	

